package cn.com.huzo.webapp.controller.admin;

import cn.com.huzo.common.util.ApplicationConfiguration;
import huzofw.common.web.controller.BaseFormController;
import huzofw.model.entities.identity.IdentityRoleEntity;
import huzofw.model.entities.identity.MemberHasRoleEntity;
import huzofw.service.security.IdentityRoleManager;
import huzofw.service.security.MemberHasRoleManager;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

/**
 * 角色管理控制器
 *
 * @author 吴旭
 */
@Controller
public class IdentityRoleController extends BaseFormController {

    // <editor-fold defaultstate="collapsed" desc="属性">
    private final String rootNodeText = ApplicationConfiguration.getCompanyName(); //"大连华纳制衣有限公司";

    @Resource(name = "IdentityRoleManager")
    private IdentityRoleManager _manager;

    @Resource(name = "MemberHasRoleManager")
    private MemberHasRoleManager _managerLines;
    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="画面处理部分">
    /**
     * 跳转到角色一览画面
     * <p>
     * @return
     */
    @RequestMapping("admin/role/list.html")
    public ModelAndView redirectListWnd() {
        ModelAndView view = new ModelAndView();
        view.setViewName("90_admin/role_list");
        return view;
    }

    /**
     * 转到更改密码画面
     * <p>
     * @return
     */
    @RequestMapping("admin/role/edit.html")
    public ModelAndView redirectEditWnd() {
        ModelAndView view = new ModelAndView();
        view.setViewName("90_admin/role_edit");
        return view;
    }
    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="角色数据处理部分">
    /**
     * 根据标识取得角色数据。
     *
     * @param key 角色标识
     * @return 角色数据。
     * @throws java.lang.Exception
     */
    @RequestMapping(value = "admin/role/fetchByKey.html",
            method = RequestMethod.POST)
    public @ResponseBody
    Map<String, Object> fetchByKey(@RequestBody Long key) throws Exception {
        _manager.setRootNodeText(rootNodeText);
        return _manager.fetchByHeaderKey(key);
    }

    /**
     * 根据条件取得系统成员分页数据。
     * <p>
     * @param filters 检索条件
     * @return 系统成员分页数据
     * @throws Exception
     */
    @RequestMapping(value = "admin/role/fetchForPage.html",
            method = RequestMethod.POST)
    public @ResponseBody
    Map<String, Object> fetchForPage(@RequestBody Map<String, Object> filters) throws Exception {
        _manager.setRootNodeText(rootNodeText);
        filters.put("RootNodeText", rootNodeText);
        return _manager.fetchForPage(filters);
    }

    /**
     * 角色数据增加或者修改
     *
     * @param instance 要处理的数据
     * @return
     *
     * @throws java.lang.Exception
     */
    @RequestMapping(value = "admin/role/save.html",
            method = RequestMethod.POST)
    public @ResponseBody
    Long save(@RequestBody final IdentityRoleEntity instance) throws Exception {
        Long rowsAffected;

        if (instance.getRoleId() != null) {
            rowsAffected = (long) _manager.update(instance);
        } else {
            rowsAffected = _manager.insert(instance);
        }

        return rowsAffected;
    }

    /**
     * 删除角色
     *
     * @param instance 要删除的数据
     * @return 影响行数
     * @throws java.lang.Exception
     */
    @RequestMapping(value = "admin/role/delete.html",
            method = RequestMethod.POST)
    public @ResponseBody
    Integer delete(@RequestBody final IdentityRoleEntity instance) throws Exception {
        return _manager.delete(instance);
    }

    /**
     * 根据用户名和角色编号取得记录数据
     *
     * @param filters 条件
     * @return 记录数
     * @throws java.lang.Exception
     */
    @RequestMapping(value = "admin/role/hasRole.html",
            method = RequestMethod.POST)
    public @ResponseBody
    Integer fetchCountByRoleNoAndUserName(@RequestBody final Map<String, Object> filters) throws Exception {
        return _managerLines.fetchCountByRoleNoAndUserName(filters);
    }
    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="角色所属成员数据处理部分">
    /**
     * 批量增加系统成员。
     *
     * @param itemList 要处理的数据列表
     * @return
     *
     * @throws java.lang.Exception
     */
    @RequestMapping(value = "admin/role/lines/insert.html",
            method = RequestMethod.POST)
    public @ResponseBody
    Integer insert(@RequestBody final List<MemberHasRoleEntity> itemList) throws Exception {
        int rowsAffected = 0;

        if (itemList.size() > 0) {
            rowsAffected = _managerLines.insert(itemList);
        }

        return rowsAffected;
    }

    /**
     * 删除角色所属用户。
     *
     * @param instance 要删除的成员
     * @return 影响行数
     * @throws java.lang.Exception
     */
    @RequestMapping(value = "admin/role/lines/delete.html",
            method = RequestMethod.POST)
    public @ResponseBody
    Integer delete(@RequestBody final MemberHasRoleEntity instance) throws Exception {
        return _managerLines.delete(instance);
    }

    /**
     * 根据角色标识取得所属用户。
     *
     * @param roleKey 角色标识
     * @return 角色所属用户
     * @throws java.lang.Exception
     */
    @RequestMapping(value = "admin/role/lines/fetchMemberByRoleKey.html",
            method = RequestMethod.POST)
    public @ResponseBody
    List<MemberHasRoleEntity> fetchMemberByRoleKey(@RequestBody final Long roleKey) throws Exception {
        return _managerLines.fetchMemberByRoleKey(roleKey);
    }
    // </editor-fold>
}
